package com.cow.sram.module.distribution;


import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cow.framework.utils.reflection.MapToBeanUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.data.entity.Orgrole;
import com.cow.sram.data.entity.Roleinfo;
import com.cow.sram.module.authority.RoleInfoService;
/**
 * 组织角色
 * @author Administrator
 *
 */
@RestController
@EnableAutoConfiguration
@Transactional
@RequestMapping(value="/distribution")
public class OrgRoleController extends BaseComponent{
	@Autowired
	OrgRoleService orgRoleService;
	@Autowired
	RoleInfoService roleInfoService;
	
	/**查询组织角色列表*/
	@RequestMapping(value="/findOrgrole")
	public Object findOrgrole(@RequestBody Map parmMap){
		List<Roleinfo> roleList=orgRoleService.findOrgrole();
		for(Roleinfo role:roleList){
			 /**判断组织是否有角色*/
			if(orgRoleService.isExist((Integer)parmMap.get("orgId"), role.getRoleid())){
				role.setGrantFlag(1);
			}else{
				role.setGrantFlag(0);
			}
		}
		return roleList;
	}
	
	/**根据ID查询组织角色*/
	@RequestMapping(value="/getOrgroleById")
	public Object getOrgroleById(@RequestParam Integer id){
		return orgRoleService.getOrgroleById(id);
	}
	
	/**新增组织角色记录*/
	@RequestMapping(value="/newOrgrole")
	public Object newOrgrole(@RequestBody Orgrole orgrl){
		orgRoleService.newOrgrole(orgrl);
		return "success";
	}
	
	/**编辑组织角色*/
	@RequestMapping(value="editOrgrole")
	public Object editOrgrole(@RequestBody Map parmMap){
		Orgrole orgrl=orgRoleService.getOrgroleById(Integer.valueOf(parmMap.get("mapid").toString()));
		MapToBeanUtil.copyMapToBean(parmMap, orgrl);
		orgRoleService.newOrgrole(orgrl);
		return "success";
	}
	
	/**删除组织角色数据*/
	@RequestMapping(value="/delOrgrole")
	public Object delOrgrole(@RequestParam Integer id){
		orgRoleService.delOrgrolrById(id);
		return "success";
	}
	

}
